<?php
namespace App\Internals;

use FoxLib\Exceptions\InternalServiceException;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;

class StudyInternal
{
    public static function getByUserId($userId)
    {
        try {
            $client = new Client();
            $rpcUrl = config('app.internal_services_host.study') . "/internal/users/{$userId}" ;
            $response = $client->request('GET', $rpcUrl);
            $result = json_decode($response->getBody(), true);
            return $result['data'];
        } catch (ClientException $e) {
            throw new InternalServiceException();
        }
    }

    public static function getAllUsersCurrent($isEnd, $page, $limit)
    {
        try {
            $client = new Client();
            $rpcUrl = config('app.internal_services_host.study') . "/internal/courses/currents";
            $response = $client->request('GET', $rpcUrl, [
                'query' => [
                    'isEnd' => $isEnd,
                    'page' => $page,
                    'limit' => $limit,
                ],
            ]);
            $result = json_decode($response->getBody(), true);
            return $result['data'];
        } catch (ClientException $e) {
            throw new InternalServiceException();
        }
    }

    // 获取一个用户某个课程中的最新课时信息
    public static function getLatestLessonByUserIdCourseId($userId, $courseId)
    {
        try {
            $client = new Client();
            $rpcUrl = config('app.internal_services_host.study') . "/internal/lessons/latest";
            $response = $client->request('GET', $rpcUrl, [
                'query' => [
                    'userId' => $userId,
                    'courseId' => $courseId,
                ],
            ]);
            $result = json_decode($response->getBody(), true);
            return $result['data'];
        } catch (ClientException $e) {
            throw new InternalServiceException();
        }
    }

    public static function lessonStore($params)
    {
        try {
            $client = new Client();
            $rpcUrl = config('app.internal_services_host.study') . "/internal/lessons";
            $response = $client->request('POST', $rpcUrl, [
                'json' => $params,
            ]);
            $result = json_decode($response->getBody(), true);
            return $result['data'];
        } catch (ClientException $e) {
            throw new InternalServiceException();
        }
    }

    public static function setCoursePushEnd($userId, $courseId)
    {
        try {
            $client = new Client();
            $rpcUrl = config('app.internal_services_host.study') . "/internal/courses/pushend";
            $response = $client->request('POST', $rpcUrl, [
                'json' => [
                    'userId' => $userId,
                    'courseId' => $courseId,
                ],
            ]);
            $result = json_decode($response->getBody(), true);
            return $result['code'];
        } catch (ClientException $e) {
            throw new InternalServiceException();
        }
    }

    public static function weeklyStatStore($params)
    {
        try {
            $client = new Client();
            $rpcUrl = config('app.internal_services_host.study') . "/internal/weeklystats";
            $response = $client->request('POST', $rpcUrl, [
                'json' => $params,
            ]);
            $result = json_decode($response->getBody(), true);
            return $result['data'];
        } catch (ClientException $e) {
            throw new InternalServiceException();
        }
    }

    // 获取用户已经推送的课时数
    public static function getCountPushedLesson($userId, $courseId)
    {
        try {
            $client = new Client();
            $rpcUrl = config('app.internal_services_host.study') . "/internal/lessons/count-pushed";
            $response = $client->request('GET', $rpcUrl, [
                'query' => [
                    'userId' => $userId,
                    'courseId' => $courseId,
                ],
            ]);
            $result = json_decode($response->getBody(), true);
            return $result['data'];
        } catch (ClientException $e) {
            throw new InternalServiceException();
        }
    }

    public static function getCourseNextLevel($userId, $level)
    {
        try {
            $client = new Client();
            $rpcUrl = config('app.internal_services_host.study') . "/internal/courses/next-level";
            $response = $client->request('GET', $rpcUrl, [
                'query' => [
                    'userId' => $userId,
                    'level' => $level,
                ],
            ]);
            $result = json_decode($response->getBody(), true);
            return $result['data'];
        } catch (ClientException $e) {
            throw new InternalServiceException();
        }
    }

    public static function getYesterdayLessons($start, $end)
    {
        try {
            $client = new Client();
            $rpcUrl = config('app.internal_services_host.study') . "/internal/lessons/learned/yesterday";
            $response = $client->request('GET', $rpcUrl, [
                'query' => [
                    'start' => $start,
                    'end' => $end,
                ],
            ]);
            $result = json_decode($response->getBody(), true);
            return $result['data'];
        } catch (ClientException $e) {
            throw new InternalServiceException();
        }
    }

    public static function getStartTimeLearn($userId)
    {
        try {
            $client = new Client();
            $rpcUrl = config('app.internal_services_host.study') . "/internal/courses/starttimelearn";
            $response = $client->request('GET', $rpcUrl, [
                'query' => [
                    'userId' => $userId,
                ],
            ]);
            $result = json_decode($response->getBody(), true);
            return $result['data'];
        } catch (ClientException $e) {
            throw new InternalServiceException();
        }
    }

}
